package com.example.navigation.api.navigation

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.navigation.api.ResponseData
import com.example.navigation.api.device.DeviceRepository
import com.example.navigation.api.device.Devices
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * Created with IntelliJ IDEA.
 * @Author: Liao Run
 * @Date: 2024-06-10 3:37
 * @Description:
 **/
@HiltViewModel
class DeviceViewModel @Inject constructor(private val deviceRepository: DeviceRepository) :
    ViewModel() {
    val timeout = MutableStateFlow(false)
    val getRes = MutableStateFlow<ResponseData<Devices>>(ResponseData(0, "", null))
    fun getDevices(id: String) {
        viewModelScope.launch {
            try {
                val res = deviceRepository.getDeviceInfo(id)
                getRes.emit(res)
            } catch (e: Exception) {
                timeout.emit(true)
            }
        }
    }
}